<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="800"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="BOM号" prop="code">
            <el-input v-model="form.code" disabled placeholder="自动生成"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工艺分组" prop="craft_category_id">
            <skDicSelect
              v-model="form.craft_category_id"
              :code="'CraftCategory'"
              :placeholder="'请选择分组'"
            >
            </skDicSelect>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>

        <!--  -->
      </el-row>
    </el-form>

    <sc-VxeTable
      :tablecode="'pro_bom_save'"
      :data="form.details"
      :gridOps="gridSubOpts"
      :gridEvts="gridEvts"
      ref="subbe"
      :sortable="true"
    >
    </sc-VxeTable>

    <!-- </sc-water-mark> -->
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import tool from "@/utils/tool";
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
// import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skAutoChoose, skDicSelect },
  data() {
    return {
      mode: "add",
      form: {
        material: {},
        lot: {},
        color: {},
        details: [],
        remark: "",
      },
      gridSubOpts: {
        height: 250,
        columns: [
          {
            field: "material.name",
            event: {
              select: ({ e, row }) => {
                row["material"] = e;
              },
            },
          },
        ],
      },
      gridEvts: {
        copydata: ({ button }) => {
          let data = tool.objCopy(button.selectRecords[0]);
          this.form.details.push(data);
        },
        beforeAddRow: (row) => {
          row.process_name = "复合" + (this.form.details.length + 1);
          row.rate = 105;
        },
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
        copy: "复制",
      },
      rules: {
        name: { required: true, message: "工艺名必填" },
        craft_category_id: { required: true, message: "必选" },
      },
      visible: false,
      isSaveing: false,
    };
  },
  watch: {
    "form.details": {
      handler(val) {
        let process_names = [];
        let remark = [];
        this.boms = val.filter((v) => {
          if (v.material?.fullname ?? null)
            process_names.push((v.material.fullname ?? "").replace(/\s+/g, ""));
          if (v.point_stick?.name ?? null)
            process_names.push((v.point_stick.name ?? "").replace(/\s+/g, ""));
          if (v.glue?.fullname ?? null)
            process_names.push((v.glue.fullname ?? "").replace(/\s+/g, ""));
          if ((v.remark ?? "") != "") remark.push(v.remark.replace(/\s+/g, ""));
        });
        this.form.name = process_names.join("+");
        this.form.remark = remark.join("|");
      },
      deep: true,
    },
  },
  mounted() {},
  methods: {
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    materialSelect(data) {
      this.form.material = data;
    },
    colorSelect(data) {
      this.form.color = data;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.pro.bom.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.visible = false;
            this.$message.success("操作成功");
            this.$emit("success");
          }
        }
      });
    },
    //表单注入数据
    setData(data, mode = "edit") {
      this.mode = mode;
      if (mode == "copy") {
        data = tool.objCopy(data);
        delete data.id;
        data.details = data.details.map((v) => {
          delete v.id;
          return v;
        });
      }
      Object.assign(this.form, tool.removeNull(data));
    },
  },
};
</script>

<style>
.attachments .el-upload--picture-card,
.attachments .el-upload-list--picture-card .el-upload-list__item {
  width: 100px;
  height: 100px;
  overflow: hidden;
}
</style>
